﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineModel;
using WineBLL;
using System.Collections;

namespace WineProm.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        /// <summary>
        /// 酒帝网首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            WineGoodsMessagesBLL bll = new WineGoodsMessagesBLL();
            return View(bll.GoodsMessages_Show());
        }
        /// <summary>
        /// 登陆页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        //登陆验证
        public JsonResult LoginDo(string UsersName,string UsersPwd)
        {
            WineLoginDoBLL bll = new WineLoginDoBLL();
            return Json(bll.Login(UsersName, UsersPwd)[0] as System.Collections.Generic.Dictionary<string, object>);
        }
        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return View();
        }
        /// <summary>
        /// 注册方法
        /// </summary>
        /// <param name="nicheng"></param>
        /// <param name="tel"></param>
        /// <param name="telpwd"></param>
        /// <returns></returns>
        public ActionResult RegisterAdo(string nicheng, string tel, int telpwd)
        {
            WineRegisterBLL bll = new WineRegisterBLL();
            WineModel.Users user =new WineModel.Users();
            user.UsersName = nicheng;
            user.UsersAccount = tel;
            user.UsersPwd = telpwd;
            if (bll.RegisterAddDo(user) > 0)
            {
                return Content("<script>alert('注册成功');location.href='/Home/Login'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢,请稍后重试！');location.href='/Home/Register'</script>");
            }
        }
        /// <summary>
        /// 高分推荐
        /// </summary>
        /// <returns></returns>
        public ActionResult Recommend()
        {
            WineRecommendBLL bll = new WineRecommendBLL();
            return View(bll.RecommendShow());
        }
        /// <summary>
        /// 名庄
        /// </summary>
        /// <returns></returns>
        public ActionResult Chateau()
        {
            return View();
        }
        /// <summary>
        /// 精品闪购
        /// </summary>
        /// <returns></returns>
        public ActionResult Flash()
        {
            WineFlashBLL bll = new WineFlashBLL();
            return View(bll.FlashShow());
        }
        /// <summary>
        /// 洋酒风尚
        /// </summary>
        /// <returns></returns>
        public ActionResult Foreign()
        {
            return View();
        }
        public ActionResult GoodsTypeSearch(int id)
        {
            WineGoodsMessagesBLL bll = new WineGoodsMessagesBLL();
            return View("Search", bll.GoodsTypeSearch(id));
        }
        public ActionResult PriceSearch(int id, decimal PriceMin, decimal PriceMax)
        {
            WineGoodsMessagesBLL bll = new WineGoodsMessagesBLL();
            return View("Search", bll.PriceSearch(PriceMin, PriceMax, id));
        }
        public ActionResult BrandSearch(int id)
        {
            WineGoodsMessagesBLL bll = new WineGoodsMessagesBLL();
            return View("Search", bll.BrandSearch(id));
        }
        /// <summary>
        /// 商品搜索
        /// </summary>
        /// <returns></returns>
        public ActionResult Search(string str)
        {
            WineGoodsMessagesBLL bll = new WineGoodsMessagesBLL();
            return View(bll.Search_Show(str));
        }
        /// <summary>
        /// 商品详情页
        /// </summary>
        /// <returns></returns>
        public ActionResult Details(int id)//根据商品编号显示商品详情
        {
            WineDetailsBLL bll = new WineDetailsBLL();
            try
            {
                ViewBag.Comments = bll.Comments_Show(id);
            }
            catch
            {
                ViewBag.Comments = null;
            }
            return View(bll.Details_Show(id));
        }
        public JsonResult DShow(int id)
        {
            WineDetailsBLL bll = new WineDetailsBLL();
            try
            {
                ViewBag.Comments = bll.Comments_Show(id);
            }
            catch
            {
                ViewBag.Comments = null;
            }
            return Json((bll.Details_Show(id)[0] as System.Collections.Generic.Dictionary<string,object>)["Description"].ToString());
        }
        //加入购物车
        public ActionResult ShopAdd(int GID, int UsersID,int Num,decimal Price)
        {
            WineDetailsBLL bll = new WineDetailsBLL();
            WineModel.Shop shop = new WineModel.Shop();
            shop.GoodsID = GID;
            shop.UsersID = UsersID;
            shop.GoodsNum = Num;
            shop.GoodsPrice = Price; //用户编号
            shop.ShopDate = DateTime.Now;
            if (bll.ShopAddDo(shop)> 0)
            {
                return Content("<script>alert('成功加入购物车');location.href='/Home/Shop/" + UsersID + "'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢,请稍后重试！');location.href='/Home/Details/" + GID + "'</script>");
            }
        }
        /// <summary>
        /// 购物车
        /// </summary>
        /// <returns></returns>
        public ActionResult Shop(int id)
        {
            WineShopBLL bll = new WineShopBLL();
            return View(bll.ShopShow(id));
        }
        /// <summary>
        /// 删除购物车信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShopDelete(int id, int UsersID)
        {
            WineShopBLL bll=new WineShopBLL ();
            if(bll.ShopDelete(id)!=0)
            {
                return Content("<script>alert('删除成功!');location.href='/Home/Shop/"+UsersID+"'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢，请稍后重试!');location.href='/Home/Shop/"+UsersID+"'</script>");
            }
        }
       
        /// <summary>
        /// 订单结算
        /// </summary>
        /// <returns></returns>
        public ActionResult Indent(int id)
        {
            WineShopBLL bll = new WineShopBLL();
            WineBLL.Consignees_ShowBLL bbll = new Consignees_ShowBLL();
            ViewBag.Areas = bbll.AreasShow();
            return View(bll.ShopShow(id));
        }
        /// <summary>
        /// 显示用户收货地址
        /// </summary>
        /// <param name="UsersID"></param>
        /// <returns></returns>
        public JsonResult Consignees_Show(int UsersID)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            return Json(bll.Consignees_Show(UsersID));
        }
        /// <summary>
        /// 删除用户收货地址
        /// </summary>
        /// <param name="ConsigneeID"></param>
        /// <returns></returns>
        public JsonResult Consignees_Del(int ConsigneeID)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            return Json(bll.Consignees_Del(ConsigneeID));
        }
        /// <summary>
        /// 添加用户收货地址
        /// </summary>
        /// <param name="Consignee"></param>
        /// <returns></returns>
        public JsonResult Consignees_Add(WineModel.Consignee Consignee)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            return Json(bll.Consignees_Add(Consignee));
        }
        /// <summary>
        /// 修改用户收货地址
        /// </summary>
        /// <param name="Consignee"></param>
        /// <returns></returns>
        public JsonResult Consignees_Edit(WineModel.Consignee Consignee)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            return Json(bll.Consignees_Edit(Consignee));
        }
        /// <summary>
        /// 显示地区列表
        /// </summary>
        /// <returns></returns>
        public JsonResult AreasShow(int UpperStory)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            return Json(bll.AreasShow().Where(p=>p.UpperStory==UpperStory).ToList());
        }
        /// <summary>
        /// 显示修改用户收货地址
        /// </summary>
        /// <returns></returns>
        public JsonResult ConsigneeSelect(int UsersID,int Cid)
        {
            System.Collections.Generic.Dictionary<string, object> ss = new Dictionary<string, object>();
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            foreach (System.Collections.Generic.Dictionary<string, object> item in bll.Consignees_Show(UsersID))
            {
                if (Convert.ToInt32(item["ConsigneeID"]) == Cid)
                {
                    ss = item;
                }
            }
            return Json(ss);
        }
        /// <summary>
        /// 显示修改地区列表
        /// </summary>
        /// <returns></returns>
        public JsonResult AreasSelect(int Aid)
        {
            WineBLL.Consignees_ShowBLL bll = new Consignees_ShowBLL();
            int UpperStory=bll.AreasSelect(Aid).FirstOrDefault().UpperStory;
            return Json(bll.AreasShow().Where(p => p.UpperStory == UpperStory).ToList());
        }
        /// <summary>
        /// 生成订单
        /// </summary>
        /// <returns></returns>
        public JsonResult AddIndent(int UsersID, int IndentNumber, decimal Payfreght, decimal PayDue, int PayMent, string LeaveWord, string Express, string InventoryID, decimal Total, int ConsigneeID)
        {
            WineIndentBLL bll = new WineIndentBLL();
            WineBLL.WineShopBLL bbll = new WineShopBLL();
            return Json(bll.AddIndent(UsersID, IndentNumber, Payfreght, PayDue, PayMent, LeaveWord, Express, InventoryID, Total, ConsigneeID, bbll.ShopShow(UsersID)));
        }
        /// <summary>
        /// 订单支付
        /// </summary>
        /// <returns></returns>
        public ActionResult Pay(decimal Price, int id)
        {
            ViewBag.Price = Price;
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 货到付款
        /// </summary>
        /// <returns></returns>
        public ActionResult Delivery()
        {
            return View();
        }
        /// <summary>
        /// 支付宝付款
        /// </summary>
        /// <returns></returns>
        public ActionResult Alipay(int Price,int id)
        {
            ViewBag.Price = Price;
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 微信付款
        /// </summary>
        /// <returns></returns>
        public ActionResult WeChatPay()
        {
            return View();
        }
        /// <summary>
        /// 用户中心
        /// </summary>
        /// <returns></returns>
        public ActionResult Center(int id)
        {
            ViewBag.id = id;
            WineIndentBLL bll = new WineIndentBLL();
            return View(bll.IndentShow(id));
        }
        /// <summary>
        /// 我的订单——用户中心
        /// </summary>
        /// <returns></returns>
        public ActionResult MyOrder(int id)
        {
            ViewBag.id = id;
            WineIndentBLL bll = new WineIndentBLL();
            return View(bll.IndentShow(id));
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public ActionResult IndentsDelete(int id,int UserID)
        {
            WineIndentBLL bll = new WineIndentBLL();
            if (bll.IndentsDelete(id) != 0)
            {
                return Content("<script>alert('删除成功!');location.href='/Home/MyOrder/" + UserID + "'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢，请稍后重试!');location.href='/Home/MyOrder/" + UserID + "'</script>");
            }
        }
        /// <summary>
        /// 基本信息——用户用心
        /// </summary>
        /// <returns></returns>
        public ActionResult Information(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 到货通知
        /// </summary>
        /// <returns></returns>
        public ActionResult AOG()
        {
            return View();
        }
        /// <summary>
        /// 商品评价
        /// </summary>
        /// <returns></returns>
        public ActionResult Evaluate(int id)
        {
            ViewBag.id = id;
            WineCommentBLL bll = new WineCommentBLL();
            return View(bll.CommentShow(id));
        }
        /// <summary>
        /// 收货地址
        /// </summary>
        /// <returns></returns>
        public ActionResult Site(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 退货记录
        /// </summary>
        /// <returns></returns>
        public ActionResult Return(int id)
        {
            ViewBag.id = id;
            WineReturnedBLL bll = new WineReturnedBLL();
            return View(bll.ReturnedShow(id));
        }
        /// <summary>
        /// 我的金币
        /// </summary>
        /// <returns></returns>
        public ActionResult Mygold(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 我的余额
        /// </summary>
        /// <returns></returns>
        public ActionResult Balance(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 我的收藏
        /// </summary>
        /// <returns></returns>
        public ActionResult Collect(int id)
        {
            ViewBag.id = id;
            WineCikkectBLL bll = new WineCikkectBLL();
            return View(bll.CikkectShow(id));
        }
        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="Goodsid"></param>
        /// <param name="Usersid"></param>
        /// <param name="cokkectdate"></param>
        /// <returns></returns>
        public ActionResult CollectAdd(int Goodsid,int Usersid,DateTime cokkectdate)
        {
            WineCikkectBLL bll = new WineCikkectBLL();
            WineModel.Cikkect cc = new Cikkect();
            cc.GoodsID = Goodsid;
            cc.UsersID = Usersid;
            cc.CollectionTime = cokkectdate;
            if (bll.CikkectAdd(cc)>0)
            {
                return Content("<script>alert('收藏成功');location.href='/Home/Collect/" + Usersid + "'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢,请稍后重试！');location.href='/Home/Shop/" + Usersid + "'</script>");
            }

        }

        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="id"></param>
        /// <param name="UsersID"></param>
        /// <returns></returns>
        public ActionResult CikkectDelete(int id, int UsersID)
        {
            WineCikkectBLL bll = new WineCikkectBLL();
            if (bll.CikkectDelete(id) != 0)
            {
                return Content("<script>alert('取消成功成功!');location.href='/Home/Collect/" + UsersID + "'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢，请稍后重试!');location.href='/Home/Collect/" + UsersID + "'</script>");
            }
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="UsersID"></param>
        /// <param name="GoodsID"></param>
        /// <param name="CommentContent"></param>
        /// <param name="CommentStar"></param>
        /// <returns></returns>
        public JsonResult AddComments(int UsersID, int GoodsID, string CommentContent, int CommentStar)
        {
            WineCommentBLL bll = new WineCommentBLL();
            return Json(bll.AddComments(UsersID, GoodsID, CommentContent, CommentStar));
        }
    }
}
